# encoding: utf-8

import mygui
import myserial
import threading
import queue
from time import sleep

class Main:
    def __init__(self):
        self.tx_queue = queue.Queue()
        self.rx_queue = queue.Queue()
        self.serial = myserial.MySerial(tx_queue=self.tx_queue, rx_queue = self.rx_queue)
        self.gui = mygui.ModbusMasterGui(serial = self.serial)

    def TransferThread(self):
        while True:
            if not self.rx_queue.empty():
                data = self.rx_queue.get()
                if data is not None:
                    hex_str = ' '.join([f"{byte:02X}" for byte in data])
                    print(f"接收到的数据: {hex_str}")  
            sleep(0.1)

    def SerialThread(self):
        threading.Thread(target = self.serial.mainloop).start()

    def GuiThread(self):
        self.gui.mainloop()
        
if __name__ == "__main__":
    app = Main()
    threading.Thread(target = app.TransferThread).start()
    app.SerialThread()
    app.GuiThread()
